package org.example.hw.service;

import java.util.List;

import javax.annotation.Resource;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.example.hw.dto.user.LoginUserDto;
import org.example.hw.model.User;
import org.example.hw.repository.UserRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true)
public class UserServiceImpl implements UserService {

	private static final Logger LOGGER = LogManager.getLogger(UserServiceImpl.class);

	@Resource
	private UserRepository userRepository;

	@Override
	public User save(User user) {

		LOGGER.info("Saving user: " + user);

		return userRepository.save(user);
	}

	@Transactional(readOnly = false)
	@Override
	public User update(User user) {

		LOGGER.info("Updating user: " + user);

		return userRepository.save(user);
	}

	@Transactional(readOnly = false)
	@Override
	public void delete(Long id) {

		LOGGER.info("Deleting user with id: " + id);

		// userRepository.delete(id);
		User user = userRepository.findOne(id);
		user.setActive(false);
	}

	@Transactional(readOnly = false)
	@Override
	public void delete(String username) {

		LOGGER.info("Deleting user with username: " + username);

		// userRepository.delete(id);
		User user = userRepository.findUserByUsername(username);
		user.setActive(false);
	}

	@Override
	public List<User> findAll() {

		LOGGER.info("Fetching all users...");

		return userRepository.findAll();
	}

	@Override
	public User findById(Long id) {

		LOGGER.info("Finding user with id: " + id);

		return userRepository.findOne(id);
	}

	@Override
	public User findByUsername(String username) {

		LOGGER.info("Finding user with username: " + username);

		return userRepository.findUserByUsername(username);
	}

	@Override
	public void search() {

		LOGGER.info("Searching...");

		userRepository.search();
	}

	@Override
	public User login(LoginUserDto loginUserDto) {

		// TODO: matching password
		// TODO: password with hash and sault in db
		// TODO: provera da li user active

		LOGGER.info("Checking in user: " + loginUserDto);

		User user = userRepository.findUserByUsername(loginUserDto.getUsername());

		if (user == null) {
			LOGGER.info("User does not exist: " + loginUserDto);

			return null;
		}

		LOGGER.info("User found: " + user);

		if (!user.getPassword().equals(loginUserDto.getPassword())) {
			LOGGER.info("Password mismatch");

			return null;
		}

		LOGGER.info("Login successful for user: " + user);

		return user;
	}

	@Transactional(readOnly = false)
	@Override
	public User register(User user) {

		LOGGER.info("Checking user..." + user);

		User existingUser = userRepository.findUserByUsername(user.getUsername());

		if (existingUser != null) {
			LOGGER.info("User with username '" + user.getUsername() + "' already exists.");

			return null;
		}

		LOGGER.info("Registering user... " + user);

		User registered = userRepository.save(user);

		LOGGER.info("User registered.");

		return registered;
	}

}
